package com.ysx.behaviour.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysx.behaviour.dto.ReadBehaviorDto;
import com.ysx.behaviour.mapper.ApReadBehaviorMapper;
import com.ysx.behaviour.pojo.ApBehaviorEntry;
import com.ysx.behaviour.pojo.ApReadBehavior;
import com.ysx.behaviour.service.ApBehaviorEntryService;
import com.ysx.behaviour.service.ApReadBehaviorService;
import com.ysx.common.constants.SystemConstants;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.exception.ToutiaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2021-10-07
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements ApReadBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public void readBehavior(ReadBehaviorDto readBehaviorDto) throws Exception {
        //1.参数校验
        if(readBehaviorDto == null || readBehaviorDto.getArticleId() == null){
            throw new ToutiaoException("文章不存在");
        }
        //2.查询行为实体
        String userInfo = RequestHeaderInfoUtils.getHeaderUserId();
        //设备
        ApBehaviorEntry entry = null;
        if(userInfo.equals("0")){
            //2.如果是匿名用户 则点赞的是设备
            entry= apBehaviorEntryService.findByUserIdOrEquipmentId(readBehaviorDto.getEquipmentId(), SystemConstants.TYPE_E);
        }else{
            //3.如果是真实的用户 则点赞的是用户
            entry= apBehaviorEntryService.findByUserIdOrEquipmentId(Integer.valueOf(userInfo), SystemConstants.TYPE_USER);
        }

        if(entry == null){
            throw new ToutiaoException("不存在的行为实体");
        }

        //3.保存或更新阅读的行为
        QueryWrapper<ApReadBehavior> queryWrapper = new QueryWrapper<ApReadBehavior>();
        queryWrapper.eq("entry_id",entry.getId());
        queryWrapper.eq("article_id",readBehaviorDto.getArticleId());

        ApReadBehavior apReadBehavior = getOne(queryWrapper);
        if(apReadBehavior == null){
            apReadBehavior = new ApReadBehavior();
            apReadBehavior.setCount(readBehaviorDto.getCount());//阅读次数
            apReadBehavior.setArticleId(readBehaviorDto.getArticleId());//文章id
            apReadBehavior.setPercentage(readBehaviorDto.getPercentage());//阅读百分比
            apReadBehavior.setEntryId(entry.getId());//实体id
            apReadBehavior.setLoadDuration(readBehaviorDto.getLoadDuration());//加载时间
            apReadBehavior.setReadDuration(readBehaviorDto.getReadDuration());//阅读时长（S)
            apReadBehavior.setCreatedTime(LocalDateTime.now());//创建时间
            save(apReadBehavior);
        }else{
            apReadBehavior.setUpdatedTime(LocalDateTime.now());
            apReadBehavior.setCount((apReadBehavior.getCount()+1));
            updateById(apReadBehavior);
        }

    }
}
